******************************************************
*Figure 2 - Party Support Timing Effect Predictions
******************************************************

*****CYCLES*******

*Set working directory
use "PartySupport_MASTER.dta"

set more off 

set seed 1234

gen AbsNominate=abs(nominate_dim1)
gen VoteShare_lag=dv_lag if party_code==100
replace VoteShare_lag=100-dv_lag if party_code==200
gen presvote=dpres if party_code==100
replace presvote=100-dpres if party_code==200

*VOTETYPES
*Regular Passage Category
gen RegPass=1 if VoteType==11 | VoteType==12 | VoteType==14 | VoteType==30
replace RegPass=0 if RegPass==.
label var RegPass "Regular Passage Category, VoteType types 11, 12, 14, 30"
*Suspensions Category
gen Susp=1 if VoteType==15 | VoteType==16 | VoteType==33 | VoteType==68
replace Susp=0 if Susp==.
label var Susp "Suspension Category, VoteType types 15, 16, 33, 68"
*Other Passage Category
gen OtherPass= 1 if VoteType==1 | VoteType==13 | VoteType==17 | VoteType==18 | VoteType==19 | VoteType==31 | VoteType==32
replace OtherPass=0 if OtherPass==.
label var OtherPass "Other Passage Category, VoteType types 1, 13, 17, 18, 19, 31, 32"
*Amendments Category
gen Amend=1 if VoteType==21 | VoteType==22 | VoteType==23 | VoteType==24 | VoteType==25 | VoteType==26 | VoteType==27 | VoteType==28 | VoteType==29 | VoteType==80
replace Amend=0 if Amend==.
label var Amend "Amendments Category, VoteType types 21, 22, 23, 24, 25, 26, 27, 28, 29, 80"
*Procedural Partisan Category
gen ProPart=1 if VoteType==61 | VoteType==76 | VoteType==77 | VoteType==79 | VoteType==81 | VoteType==82 | VoteType==88 | VoteType==89 | VoteType==93 | VoteType==95 | VoteType==97 | VoteType==99
replace ProPart=0 if ProPart==.
label var ProPart "Procedural Partisan Category, VoteType types 61, 76, 77, 79, 81, 82, 88, 89, 93, 95, 97, 99"
*Miscellaneous Procedural Category
gen MiscPro=1 if RegPass==0 & Susp==0 & OtherPass==0 & Amend==0 & ProPart==0
replace MiscPro=0 if MiscPro==.
label var MiscPro "Miscellaneous Procedural Category, VoteType types: All others"

gen DaysCycle=DaysToElection*Cycle
egen distgroup=group(state_abbrev district_code)

*MODEL:

logit PartyUnityVote DaysToElection Cycle DaysCycle DaysToElection2 DaysToElection3 presvote QualChall QualChall_lag Freshman InpartyMidterm PresApproval_In rdi_q3_In PartyOfPres PrimaryChall redist seniorit Retirement_Jacob AbsNominate VoteShare_lag RegPass Susp MiscPro Amend ProPart if PresPartyUnity==1, vce(cluster distgroup)

drawnorm M1_b1-M1_b25, n(10000) means(e(b)) cov(e(V)) clear

*Enter values of variables

forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar Cycle1=1
scalar Cycle32=32
scalar daysCycle1=days*Cycle1
scalar daysCycle32=days*Cycle32
scalar presvote1=57.4133
scalar presvote32=63.64906
scalar QualChall1=.1802485
scalar QualChall32=.098179
scalar QualChall_lag1=.2153619
scalar QualChall_lag32=.1419985
scalar Freshman1=.2118113
scalar Freshman32=.2537902
scalar InpartyMidterm1=0
scalar InpartyMidterm32=.5281861
scalar PresApproval_In1=-1.118804
scalar PresApproval_In32=-.994198
scalar rdi_q3_In1=-.2435044
scalar rdi_q3_In32=.3418647
scalar PartyOfPres1=.465401
scalar PartyOfPres32=.5460493
scalar PrimaryChall1=.3527906 
scalar PrimaryChall32=.3716194 
scalar redist1=.0093636 
scalar redist32=.0425769  
scalar seniorit1=6.168478
scalar seniorit32=5.573624
scalar Retirement_Jacob1=.0690955
scalar Retirement_Jacob32=.1415856
scalar AbsNominate1=.2828798
scalar AbsNominate32=.4427426 
scalar VoteShare_lag1=67.86514
scalar VoteShare_lag32=63.71035
scalar RegPass1=.4094659
scalar RegPass32=.0564774
scalar Susp1=.09395
scalar Susp32=.05562
scalar MiscPro1=.0469287
scalar MiscPro32=.7091023
scalar Amend1=.114091
scalar Amend32=.0633329
scalar ProPart1=.2617091
scalar ProPart32=.1068932
scalar constant=1
generate x_betahatA_`i'=M1_b1*days + M1_b2*Cycle1 + M1_b3*daysCycle1 + M1_b4*days2 + M1_b5*days3 + M1_b6*presvote1 + M1_b7*QualChall1 + M1_b8*QualChall_lag1 + M1_b9*Freshman1 + M1_b10*InpartyMidterm1 + M1_b11*PresApproval_In1 + M1_b12*rdi_q3_In1 + M1_b13*PartyOfPres1 + M1_b14*PrimaryChall1 + M1_b15*redist1 + M1_b16*seniorit1 + M1_b17*Retirement_Jacob1 + M1_b18*AbsNominate1 + M1_b19*VoteShare_lag1 + M1_b20*RegPass1 + M1_b21*Susp1 + M1_b22*MiscPro1 + M1_b23*Amend1 + M1_b24*ProPart1 + M1_b25*constant
generate predA_`i'=1/(1+exp(-(x_betahatA_`i')))
drop x_betahatA_`i'
}

forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar Cycle1=1
scalar Cycle32=32
scalar daysCycle1=days*Cycle1
scalar daysCycle32=days*Cycle32
scalar presvote1=57.4133
scalar presvote32=63.64906
scalar QualChall1=.1802485
scalar QualChall32=.098179
scalar QualChall_lag1=.2153619
scalar QualChall_lag32=.1419985
scalar Freshman1=.2118113
scalar Freshman32=.2537902
scalar InpartyMidterm1=0
scalar InpartyMidterm32=.5281861
scalar PresApproval_In1=-1.118804
scalar PresApproval_In32=-.994198
scalar rdi_q3_In1=-.2435044
scalar rdi_q3_In32=.3418647
scalar PartyOfPres1=.465401
scalar PartyOfPres32=.5460493
scalar PrimaryChall1=.3527906 
scalar PrimaryChall32=.3716194 
scalar redist1=.0093636 
scalar redist32=.0425769  
scalar seniorit1=6.168478
scalar seniorit32=5.573624
scalar Retirement_Jacob1=.0690955
scalar Retirement_Jacob32=.1415856
scalar AbsNominate1=.2828798
scalar AbsNominate32=.4427426 
scalar VoteShare_lag1=67.86514
scalar VoteShare_lag32=63.71035
scalar RegPass1=.4094659
scalar RegPass32=.0564774
scalar Susp1=.09395
scalar Susp32=.05562
scalar MiscPro1=.0469287
scalar MiscPro32=.7091023
scalar Amend1=.114091
scalar Amend32=.0633329
scalar ProPart1=.2617091
scalar ProPart32=.1068932
scalar constant=1
generate x_betahatB_`i'=M1_b1*days + M1_b2*Cycle32 + M1_b3*daysCycle32 + M1_b4*days2 + M1_b5*days3 + M1_b6*presvote32 + M1_b7*QualChall32 + M1_b8*QualChall_lag32 + M1_b9*Freshman32 + M1_b10*InpartyMidterm32 + M1_b11*PresApproval_In32 + M1_b12*rdi_q3_In32 + M1_b13*PartyOfPres32 + M1_b14*PrimaryChall32 + M1_b15*redist32 + M1_b16*seniorit32 + M1_b17*Retirement_Jacob32 + M1_b18*AbsNominate32 + M1_b19*VoteShare_lag32 + M1_b20*RegPass32 + M1_b21*Susp32 + M1_b22*MiscPro32 + M1_b23*Amend32 + M1_b24*ProPart32 + M1_b25*constant
generate predB_`i'=1/(1+exp(-(x_betahatB_`i')))
drop x_betahatB_`i'
}

foreach var of varlist predA_1-predA_735{
egen `var'a=clsort(`var')
}

foreach var of varlist predB_1-predB_735{
egen `var'b=clsort(`var')
}

save "Figure2a_Values.dta"

drop M1_b1-predB_735
gen days=_n
*Keep the 8.25th and 91.75th percentiles
keep if days==825 | days==9175
xpose, clear
rename v1 LowBound
rename v2 UpBound
drop in 1471
gen days=_n
move days LowBound
gen Type="Early Cycle" if days<736
replace Type="Late Cycle" if days>735
replace days=days-735 if days>735

*sort days

save "Figure2a_Preds.dta"

*twoway (scatter LowBound days if Type=="Early Cycle") (scatter UpBound days if Type=="Early Cycle") (scatter LowBound days if Type=="Late Cycle") (scatter UpBound days if Type=="Late Cycle")
clear
*Reshape for figure
use "Figure2a_Preds.dta"
gen shaper=1 if Type=="Early Cycle"
replace shaper=32 if Type=="Late Cycle"
drop Type
reshape wide LowBound UpBound, i(days) j(shaper)
save "Figure2a_Preds_reshaped.dta"
clear



*****POST-104 INDICATOR*******

*Set working directory
use "PartySupport_MASTER.dta"

set more off 

set seed 1234

gen AbsNominate=abs(nominate_dim1)
gen VoteShare_lag=dv_lag if party_code==100
replace VoteShare_lag=100-dv_lag if party_code==200
gen presvote=dpres if party_code==100
replace presvote=100-dpres if party_code==200

*VOTETYPES
*Regular Passage Category
gen RegPass=1 if VoteType==11 | VoteType==12 | VoteType==14 | VoteType==30
replace RegPass=0 if RegPass==.
label var RegPass "Regular Passage Category, VoteType types 11, 12, 14, 30"
*Suspensions Category
gen Susp=1 if VoteType==15 | VoteType==16 | VoteType==33 | VoteType==68
replace Susp=0 if Susp==.
label var Susp "Suspension Category, VoteType types 15, 16, 33, 68"
*Other Passage Category
gen OtherPass= 1 if VoteType==1 | VoteType==13 | VoteType==17 | VoteType==18 | VoteType==19 | VoteType==31 | VoteType==32
replace OtherPass=0 if OtherPass==.
label var OtherPass "Other Passage Category, VoteType types 1, 13, 17, 18, 19, 31, 32"
*Amendments Category
gen Amend=1 if VoteType==21 | VoteType==22 | VoteType==23 | VoteType==24 | VoteType==25 | VoteType==26 | VoteType==27 | VoteType==28 | VoteType==29 | VoteType==80
replace Amend=0 if Amend==.
label var Amend "Amendments Category, VoteType types 21, 22, 23, 24, 25, 26, 27, 28, 29, 80"
*Procedural Partisan Category
gen ProPart=1 if VoteType==61 | VoteType==76 | VoteType==77 | VoteType==79 | VoteType==81 | VoteType==82 | VoteType==88 | VoteType==89 | VoteType==93 | VoteType==95 | VoteType==97 | VoteType==99
replace ProPart=0 if ProPart==.
label var ProPart "Procedural Partisan Category, VoteType types 61, 76, 77, 79, 81, 82, 88, 89, 93, 95, 97, 99"
*Miscellaneous Procedural Category
gen MiscPro=1 if RegPass==0 & Susp==0 & OtherPass==0 & Amend==0 & ProPart==0
replace MiscPro=0 if MiscPro==.
label var MiscPro "Miscellaneous Procedural Category, VoteType types: All others"

egen distgroup=group(state_abbrev district_code)

gen post104=1 if congress>=104
replace post104=0 if post104==.

gen DaysPost=DaysToElection*post104

*MODELS:

logit PartyUnityVote DaysToElection post104 DaysPost DaysToElection2 DaysToElection3 presvote QualChall QualChall_lag Freshman InpartyMidterm PresApproval_In rdi_q3_In PartyOfPres PrimaryChall redist seniorit Retirement_Jacob AbsNominate VoteShare_lag RegPass Susp MiscPro Amend ProPart if PresPartyUnity==1, vce(cluster distgroup)

drawnorm M1_b1-M1_b25, n(10000) means(e(b)) cov(e(V)) clear

*Enter values of variables

forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar post0=0
scalar post1=1
scalar dayspost0=days*post0
scalar dayspost1=days*post1
scalar presvote0=54.71494
scalar presvote1=60.16917
scalar QualChall0=.2067712  
scalar QualChall1=.1745056 
scalar QualChall_lag0=.2137116 
scalar QualChall_lag1=.1782461 
scalar Freshman0=.2761097 
scalar Freshman1=.2483254 
scalar InpartyMidterm0=.2692509 
scalar InpartyMidterm1=.2416295 
scalar PresApproval_In0=-.5279418 
scalar PresApproval_In1=-.0929461 
scalar rdi_q3_In0=-.1409507 
scalar rdi_q3_In1=-.0091844 
scalar PartyOfPres0=.4831943 
scalar PartyOfPres1=.4924144 
scalar PrimaryChall0=.3364012 
scalar PrimaryChall1=.3641371 
scalar redist0=.2305249 
scalar redist1=.1919005  
scalar seniorit0=5.983983 
scalar seniorit1=5.556118 
scalar Retirement_Jacob0=.0992975 
scalar Retirement_Jacob1=.093317 
scalar AbsNominate0=.310866 
scalar AbsNominate1=.4097119  
scalar VoteShare_lag0=66.4056 
scalar VoteShare_lag1=62.99798 
scalar RegPass0=.2608976 
scalar RegPass1=.1137361 
scalar Susp0=.0977779 
scalar Susp1=.1367108 
scalar MiscPro0=.1078801 
scalar MiscPro1=.124716 
scalar Amend0=.3067384 
scalar Amend1=.3196689 
scalar ProPart0=.1741927 
scalar ProPart1=.2097449 
scalar constant=1
generate x_betahatA_`i'=M1_b1*days + M1_b2*post0 + M1_b3*dayspost0 + M1_b4*days2 + M1_b5*days3 + M1_b6*presvote0 + M1_b7*QualChall0 + M1_b8*QualChall_lag0 + M1_b9*Freshman0 + M1_b10*InpartyMidterm0 + M1_b11*PresApproval_In0 + M1_b12*rdi_q3_In0 + M1_b13*PartyOfPres0 + M1_b14*PrimaryChall0 + M1_b15*redist0 + M1_b16*seniorit0 + M1_b17*Retirement_Jacob0 + M1_b18*AbsNominate0 + M1_b19*VoteShare_lag0 + M1_b20*RegPass0 + M1_b21*Susp0 + M1_b22*MiscPro0 + M1_b23*Amend0 + M1_b24*ProPart0 + M1_b25*constant
generate predA_`i'=1/(1+exp(-(x_betahatA_`i')))
drop x_betahatA_`i'
}

forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar post0=0
scalar post1=1
scalar dayspost0=days*post0
scalar dayspost1=days*post1
scalar presvote0=54.71494
scalar presvote1=60.16917
scalar QualChall0=.2067712  
scalar QualChall1=.1745056 
scalar QualChall_lag0=.2137116 
scalar QualChall_lag1=.1782461 
scalar Freshman0=.2761097 
scalar Freshman1=.2483254 
scalar InpartyMidterm0=.2692509 
scalar InpartyMidterm1=.2416295 
scalar PresApproval_In0=-.5279418 
scalar PresApproval_In1=-.0929461 
scalar rdi_q3_In0=-.1409507 
scalar rdi_q3_In1=-.0091844 
scalar PartyOfPres0=.4831943 
scalar PartyOfPres1=.4924144 
scalar PrimaryChall0=.3364012 
scalar PrimaryChall1=.3641371 
scalar redist0=.2305249 
scalar redist1=.1919005  
scalar seniorit0=5.983983 
scalar seniorit1=5.556118 
scalar Retirement_Jacob0=.0992975 
scalar Retirement_Jacob1=.093317 
scalar AbsNominate0=.310866 
scalar AbsNominate1=.4097119  
scalar VoteShare_lag0=66.4056 
scalar VoteShare_lag1=62.99798 
scalar RegPass0=.2608976 
scalar RegPass1=.1137361 
scalar Susp0=.0977779 
scalar Susp1=.1367108 
scalar MiscPro0=.1078801 
scalar MiscPro1=.124716 
scalar Amend0=.3067384 
scalar Amend1=.3196689 
scalar ProPart0=.1741927 
scalar ProPart1=.2097449 
scalar constant=1
generate x_betahatB_`i'=M1_b1*days + M1_b2*post1 + M1_b3*dayspost1 + M1_b4*days2 + M1_b5*days3 + M1_b6*presvote1 + M1_b7*QualChall1 + M1_b8*QualChall_lag1 + M1_b9*Freshman1 + M1_b10*InpartyMidterm1 + M1_b11*PresApproval_In1 + M1_b12*rdi_q3_In1 + M1_b13*PartyOfPres1 + M1_b14*PrimaryChall1 + M1_b15*redist1 + M1_b16*seniorit1 + M1_b17*Retirement_Jacob1 + M1_b18*AbsNominate1 + M1_b19*VoteShare_lag1 + M1_b20*RegPass1 + M1_b21*Susp1 + M1_b22*MiscPro1 + M1_b23*Amend1 + M1_b24*ProPart1 + M1_b25*constant
generate predB_`i'=1/(1+exp(-(x_betahatB_`i')))
drop x_betahatB_`i'
}

foreach var of varlist predA_1-predA_735{
egen `var'a=clsort(`var')
}

foreach var of varlist predB_1-predB_735{
egen `var'b=clsort(`var')
}

save "Figure2b_Values.dta"

drop M1_b1-predB_735
gen days=_n
*Keep the 8.25th and 91.75th percentiles
keep if days==825 | days==9175
xpose, clear
rename v1 LowBound
rename v2 UpBound
drop in 1471
gen days=_n
move days LowBound
gen Type="Early Cycle" if days<736
replace Type="Late Cycle" if days>735
replace days=days-735 if days>735

*sort days

save "Figure2b_Preds.dta"

*twoway (scatter LowBound days if Type=="Early Cycle") (scatter UpBound days if Type=="Early Cycle") (scatter LowBound days if Type=="Late Cycle") (scatter UpBound days if Type=="Late Cycle")
clear
*Reshape for figure
use "Figure2b_Preds.dta"
gen shaper=1 if Type=="Early Cycle"
replace shaper=32 if Type=="Late Cycle"
drop Type
reshape wide LowBound UpBound, i(days) j(shaper)
save "Figure2b_Preds_reshaped.dta"
clear
